package com.ivanceras.client.view;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.shared.HasHandlers;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.Widget;
import com.ivanceras.client.Choice;

public class ChoiceViewer implements HasHandlers{
	private HandlerManager handlerManager;
	public Choice choice;
	QuestionViewer questionViewer;
	
	public ChoiceViewer(Choice choice){
		this.choice = choice;
	}
	public Widget getWidget(){
		final HorizontalPanel hpanel = new HorizontalPanel();
		HorizontalPanel choicePanel = new HorizontalPanel();
		choicePanel.add(hpanel);
		//choicePanel.setBorderWidth(1);
		//hpanel.setBorderWidth(1);
		final RadioButton radioButton = new RadioButton("q"+choice.itemNo);
		hpanel.add(radioButton);
		HTML choiceText = new HTML(choice.text);
		choiceText.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		choiceText.setStyleName("choiceText");
		//TODO:need to set all others as not the correct answer 
		choiceText.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				radioButton.setValue(true);
				questionViewer.setUserAnswer(choice);
				//choice.setAnswer(true);
			}
			
		});
		radioButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				radioButton.setValue(true);
				questionViewer.setUserAnswer(choice);
				//choice.setAnswer(true);
			}
			
		});
		hpanel.add(choiceText);
		hpanel.setStyleName("choices");
		hpanel.setWidth("100%");
		hpanel.setCellWidth(choiceText, "99%");
		hpanel.setCellWidth(radioButton, "1%");
		choiceText.addMouseOverHandler(new MouseOverHandler(){

			@Override
			public void onMouseOver(MouseOverEvent event) {
				hpanel.setStyleName("choices-hover");
			}
			
		});
		choiceText.addMouseOutHandler(new MouseOutHandler(){

			@Override
			public void onMouseOut(MouseOutEvent event) {
				hpanel.setStyleName("choices");
			}
			
		});
		radioButton.addMouseOverHandler(new MouseOverHandler(){

			@Override
			public void onMouseOver(MouseOverEvent event) {
				hpanel.setStyleName("choices-hover");
			}
			
		});
		radioButton.addMouseOutHandler(new MouseOutHandler(){

			@Override
			public void onMouseOut(MouseOutEvent event) {
				hpanel.setStyleName("choices");
			}
			
		});
		hpanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		choicePanel.setWidth("100%");
		choicePanel.setCellHeight(hpanel, "99%");
		return choicePanel;
	}
	@Override
	public void fireEvent(GwtEvent<?> event) {
	    if (handlerManager != null) {
	        handlerManager.fireEvent(event);
	      }
		
	}

}
